package org.example.day8;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;
import java.util.Objects;

/**
 * @Description TODO
 * @Author 乐哥聊编程
 * @Date 2023/1/19 05:23
 */
public class CustomPartition implements Partitioner {
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        if (Objects.nonNull(value)) {
            int partitions = cluster.availablePartitionsForTopic(topic).size();
            int partition = (int) value % partitions;
            System.out.println(String.format("当前值:%s,选择分区:%s", value,partition));
            return partition;
        }
        System.out.println("使用默认分区0");
        return 0;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
